﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.ProcurementsCommand;
using Yitter.IdGenerator;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 开票通知单的添加命令处理方法
    /// </summary>
    public class InvoiceSettlementEntityAddCommandHandler : IRequestHandler<InvoiceSettlementEntityAddCommand, APIResult<object>>
    {
        /// <summary>
        /// 开票通知单仓储接口
        /// </summary>
        private readonly IBaseRepository<InvoiceSettlementEntity> iRepo;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="iRepo"></param>
        /// <param name="mapper"></param>
        public InvoiceSettlementEntityAddCommandHandler(IBaseRepository<InvoiceSettlementEntity>iRepo,IMapper mapper)
        {
            this.iRepo = iRepo;
            this.mapper = mapper;
        }


        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<object>> Handle(InvoiceSettlementEntityAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.添加成功;
            result.Msg = APIEnum.添加成功.ToString();
            var model = mapper.Map<InvoiceSettlementEntity>(request);
            var r = await iRepo.Add(model);
            if (r > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = model;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
            }

            return await Task.FromResult(result);
        }


    }
}
